const app = getApp();
Page({
  data: {
    pets: [],
    selectedPet: null,
    petIndex: null,
    date: '',
    startDate: '',
  },
  onLoad(options) {
    var that = this;
    var openid = wx.getStorageSync('openid')
    wx.request({
      url: 'http://127.0.0.1:7001/api/user/petinfo',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'GET',
      data: {
        ownerid: openid
      },
      success: function (res) {
        console.log(res.data);
        const data = res.data.data.result
        // console.log(data);
        for (var i = 0; i < data.length; i++) {
          const {
            id,
            name,
            src
          } = data[i]
          const base64 = wx.arrayBufferToBase64(new Uint8Array(src.data));
          const image = 'data:image/png;base64,' + base64
          var pets = that.data.pets;
          pets.push({
            id: id,
            name: name,
            image: image
          })
          // console.log(pets);
          that.setData({
            pets: pets
          })
        }
      }
    })
    const now = new Date()
    const data = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate()
    that.setData({
      date: data
    })

  },
  onReady() {

  },
  bindPetChange: function (e) {
    // console.log(e.detail.value);
    var selectedObject = this.data.pets[e.detail.value]
    console.log(selectedObject);
    this.setData({
      petIndex: e.detail.value,
      selectedPet: selectedObject
    })
  },

  onDateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  onSubmit() {
    var openid = wx.getStorageSync('openid')
    // const book = []
    // book.push({ownerid:openid,petid:this.data.selectedPet.id,booktime:this.data.date,employeeid:null,status:0})
    // console.log(book[0]);
    wx.request({
      url: 'http://127.0.0.1:7001/api/user/book',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      data: {
        ownerid: openid,
        petid: this.data.selectedPet.id,
        booktime: this.data.date,
        // employeeid: '',
        status: 0
      },
      success: function (res) {
        console.log(res.data);
        wx.redirectTo({
          url: '../home',
        })
      }
    })
  },
})